var searchVue = new Vue({
	el: "#app",
	data: {
		isResult: false,
		history: [],
		products: [],
		searchContent: null,
		type: "",
		page: 1,
		isEmpty: false,
		isExcute:false,
		isBottom:false
	},
	methods: {
		search: function() {
			if (searchVue.searchContent === "") {
				return;
			}
			if(this.history==null){
				this.history = new Array();
				this.history.push(searchVue.searchContent);
			}else{
				var flag = true;
				this.history.forEach(function(e){
					if(e==searchVue.searchContent){
						flag = false;
					}
				});
				if(flag){
					this.history.push(searchVue.searchContent);
				}
			}
			localStorage.setItem("search_histroy", JSON.stringify(this.history));
			wx.showLoading({
				title: "加载中",
				mask: !0
			});
			this.isResult = true;
			if (this.type == 2) {
				this.$http.post(baseUrl + "wap/product/index", {
					currentPage: this.page,
					pageSize: 10,
					name: this.searchContent
				}).then(function(t) {
					wx.hideLoading();
					var a = t.data;
					var i = a.data[0];
					if (!a.success) {
						return wx.hideLoading(), wx.showToast({
							title: a.description,
							icon: "none",
							duration: 2e3,
							mask: !0
						});
					} else {
						if (a.data.length > 0 && 1 == this.page) {
							a.data.forEach(function(e) {
								e.homePath = e.logoPath.split(",")[0];
							})
							this.products = a.data;
						} else if (a.data.length == 0 && 1 == this.page) {
							this.isEmpty = true;
						} else if (a.data.length == 0 && this.page > 1) {
							return wx.showToast({
								title: "已经到最底下了",
								icon: "none",
								duration: 1e3,
								mask: !0
							}), this.isBottom = true;
						} else {
							a.data.forEach(function(e) {
								e.homePath = e.logoPath.split(",")[0];
								this.products.push(e);
							})
						}

					}
				}).catch(function(t) {
					console.error(t);
					wx.hideLoading(), wx.showToast({
						title: "网络错误，请稍后重试",
						icon: "none",
						duration: 2e3,
						mask: !1
					});
				});
			}

		},
		handleScroll: function() {
			// 防止重复
			if (this.isExcute) {
				return;
			}
			if (this.isBottom) {
				return;
			}
			var flag = srollEvent.isReachBottom();
			if (!flag) {
				return;
			}
			this.isExcute = true;
			this.page++;
			this.search();
		},
		searchHistory:function(item){
			this.searchContent = item;
			this.search();
		},
		toDetail:function(item){
			Jump.to(page.product.detail,"guid="+item.productGuid);
			return;
		}
	},
	mounted: function() {
		window.addEventListener("scroll", this.handleScroll, {
			passive: false
		});
	},
	created: function() {
		var type = URLParam.get("type");
		this.type = type;
		var search_histroy = localStorage.getItem("search_histroy");
		this.history = JSON.parse(search_histroy);
	}
});
